package com.example.yubo.zyfypt_403yb.model;


import com.example.yubo.zyfypt_403yb.bean.ArticleBean;
import com.example.yubo.zyfypt_403yb.bean.ProjectBean;
import com.example.yubo.zyfypt_403yb.iface.ArticleIface;
import com.example.yubo.zyfypt_403yb.iface.ArticleListener;
import com.example.yubo.zyfypt_403yb.iface.ProjectIface;
import com.example.yubo.zyfypt_403yb.iface.ProjectListener;
import com.example.yubo.zyfypt_403yb.service.ArticleService;
import com.example.yubo.zyfypt_403yb.service.ProjectService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


//model 获取文章数据
public class ArticleModel implements ArticleIface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public ArticleModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据



    public void getResoutList(String project, int page, String sessionID, final ArticleListener articleListener) {
        //使用Retrofit-2
        ArticleService articleService=retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call
                =articleService.getResult(project,page,sessionID);
        //使用Retrofit-3
        call.enqueue(new Callback<List<ArticleBean>>() {

            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    articleListener.onResponse(response.body());

                }
                else
                    articleListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
               articleListener.onFail(t.toString());
            }
        });
    }
}
